package core.views;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.SystemColor;
import core.Runner;

public class RiverPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField txtTop;
	private JTextField txtMiddle;
	private JTextField txtBottom;
	private Runner runner;
	private ferryActionListener ferryListener;
	private fordActionListener fordListener;
	private caulkActionListener caulkListener;
	private JButton btnFerry;
	private JTextField textField;
	
	public RiverPanel(Runner runner) {
		this.runner = runner;
		btnFerry = new JButton("Ferry");
		btnFerry.setBounds(1, 138, 127, 102);
		ferryListener = new ferryActionListener();
		btnFerry.addActionListener(ferryListener);
		setLayout(null);
		add(btnFerry);
		
		JButton btnFord = new JButton("Ford");
		fordListener = new fordActionListener();
		btnFord.addActionListener(fordListener);
		btnFord.setBounds(138, 138, 131, 102);
		add(btnFord);
		
		JButton btnCaulk = new JButton("Caulk");
		caulkListener = new caulkActionListener();
		btnCaulk.setBounds(277, 138, 163, 102);
		add(btnCaulk);
		btnCaulk.addActionListener(caulkListener);
		
		txtTop = new JTextField();
		txtTop.setEditable(false);
		txtTop.setBorder(null);
		txtTop.setBackground(SystemColor.control);
		txtTop.setText("Yo dawg I heard you like rivers");
		txtTop.setBounds(125, 22, 176, 20);
		add(txtTop);
		txtTop.setColumns(10);
		
		txtMiddle = new JTextField();
		txtMiddle.setEditable(false);
		txtMiddle.setBorder(null);
		txtMiddle.setBackground(SystemColor.control);
		txtMiddle.setText("Yeah i don't have anything, just");
		txtMiddle.setColumns(10);
		txtMiddle.setBounds(125, 41, 176, 20);
		add(txtMiddle);
		
		txtBottom = new JTextField();
		txtBottom.setEditable(false);
		txtBottom.setBorder(null);
		txtBottom.setBackground(SystemColor.control);
		txtBottom.setText("pick an option and get on with it");
		txtBottom.setColumns(10);
		txtBottom.setBounds(125, 60, 176, 20);
		add(txtBottom);
		
		textField = new JTextField();
		textField.setBorder(null);
		textField.setBackground(SystemColor.control);
		textField.setText("$200");
		textField.setBounds(42, 107, 86, 20);
		add(textField);
		textField.setColumns(10);
	}
	
	/***
	 * Figures out if a river crossing event will be raised
	 * 
	 * @return The boolean that determines if a crossing event will be raised.
	 */
	public boolean generateEvent()
	{
		return true;
	}
	private class ferryActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) {
			if(runner.getGroup().getMoney()<=200)
			{
				JOptionPane.showMessageDialog(null,"Not enough money.....problem?");
				btnFerry.setVisible(false);
			}
			else
			{
				runner.getGroup().setMoney((runner.getGroup().getMoney())-200);
				StartPanelMain.changeCard("Trail");
				JOptionPane.showMessageDialog(null,"Congratulations! You have crossed the river.");
			}
		}
	}
	
	/**
	 * 
	 * Inner class for 
	 *
	 */
	private class fordActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) {
			if(generateEvent())
			{
				String temp = runner.getGroup().getWagon().getInventory().dropRand(.8);
				JOptionPane.showMessageDialog(null,temp);
			}
			StartPanelMain.changeCard("Trail");
		}
	}
	
	private class caulkActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(generateEvent())
			{
				String temp = runner.getGroup().getWagon().getInventory().dropRand(.9);
				JOptionPane.showMessageDialog(null,temp);
			}
			StartPanelMain.changeCard("Trail");
			}
		}
	}


